#include "plotspanel.h"

PlotsPanel::PlotsPanel( QWidget* parent ) : QWidget( parent )
{
    this->initView();
}

void PlotsPanel::initView()
{
    this->m_CurrPlot  = new QCustomPlot( this );
    this->m_VoltPlot  = new QCustomPlot( this );
    this->m_GroupBox  = new QGroupBox( this );
    QLabel* currLabel = new QLabel( "母线滤波 输出电流(A)", this );
    QLabel* voltLabel = new QLabel( "母线滤波 输出电压(V)", this );
    currLabel->setStyleSheet( "QLabel{color:#ffffff;font-size:18px;}" );
    currLabel->setAlignment( Qt::AlignCenter );
    voltLabel->setStyleSheet( "QLabel{color:#ffffff;font-size:18px;}" );
    voltLabel->setAlignment( Qt::AlignCenter );
    this->m_GroupBox->setStyleSheet( QString( "QGroupBox{border:0px solid #79716b;border-radius:6px;background:#433e3a;}" ) );
    this->setupPlot( this->m_CurrPlot, "", "" );
    this->setupPlot( this->m_VoltPlot, "", "" );

    QVBoxLayout* currLay  = new QVBoxLayout;
    QVBoxLayout* voltLay  = new QVBoxLayout;
    QHBoxLayout* groupLay = new QHBoxLayout;

    currLay->addWidget( this->m_CurrPlot );
    currLay->addWidget( currLabel );
    voltLay->addWidget( this->m_VoltPlot );
    voltLay->addWidget( voltLabel );
    groupLay->addLayout( currLay );
    groupLay->addLayout( voltLay );

    m_GroupBox->setLayout( groupLay );

    QVBoxLayout* layout = new QVBoxLayout;
    layout->addWidget( this->m_GroupBox );
    layout->setContentsMargins( 0, 0, 0, 0 );
    this->setLayout( layout );
    this->setFixedSize( 600, 306 );
}

void PlotsPanel::setupPlot( QCustomPlot* plot, QString xLabel, QString yLabel )
{
    QFont font;

    // Set font style.
    font.setPointSize( 6 );
    font.styleHint();
    font.setStyleStrategy( QFont::PreferAntialias );
    plot->xAxis->setTickLabelFont( font );
    plot->yAxis->setTickLabelFont( font );
    plot->legend->setFont( font );

    // set some pens, brushes and backgrounds:
    plot->setFixedSize( 280, 250 );
    QLinearGradient gradient( 0, 0, 0, 400 );
    gradient.setColorAt( 0, QColor( "#eab427" ) );
    plot->xAxis->setBasePen( QPen( Qt::white, 1 ) );
    plot->yAxis->setBasePen( QPen( Qt::white, 1 ) );
    plot->xAxis->setTickPen( QPen( Qt::white, 1 ) );
    plot->yAxis->setTickPen( QPen( Qt::white, 1 ) );
    plot->xAxis->setSubTickPen( QPen( Qt::white, 1 ) );
    plot->yAxis->setSubTickPen( QPen( Qt::white, 1 ) );
    plot->xAxis->setTickLabelColor( Qt::white );
    plot->yAxis->setTickLabelColor( Qt::white );
    plot->xAxis->grid()->setPen( QPen( QColor( 140, 140, 140 ), 1, Qt::DotLine ) );
    plot->yAxis->grid()->setPen( QPen( QColor( 140, 140, 140 ), 1, Qt::DotLine ) );
    plot->xAxis->grid()->setSubGridPen( QPen( QColor( 80, 80, 80 ), 1, Qt::DotLine ) );
    plot->yAxis->grid()->setSubGridPen( QPen( QColor( 80, 80, 80 ), 1, Qt::DotLine ) );
    plot->xAxis->grid()->setSubGridVisible( true );
    plot->yAxis->grid()->setSubGridVisible( false );
    plot->xAxis->grid()->setZeroLinePen( Qt::NoPen );
    plot->yAxis->grid()->setZeroLinePen( Qt::NoPen );
    plot->xAxis->setUpperEnding( QCPLineEnding::esSpikeArrow );
    plot->yAxis->setUpperEnding( QCPLineEnding::esSpikeArrow );
    plot->yAxis->setLabel( yLabel );
    plot->xAxis->setLabel( xLabel );
    plot->xAxis->ticker()->setTickCount( 8 );

    QLinearGradient plotGradient;
    plotGradient.setStart( 0, 0 );
    plotGradient.setFinalStop( 0, 350 );
    plotGradient.setColorAt( 0, QColor( "#433e3a" ) );
    plotGradient.setColorAt( 1, QColor( "#433e3a" ) );
    plot->setBackground( plotGradient );
}
